#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
高级分析系统 - 动态筛选TOP10股票
实现智能筛选：第6步改为TOP10，第7步预测上涨概率<70%的股票自动去除
如果不足10只，自动补充；如果第5步数据用完，回到第2步重新筛选
"""

import sys
import os
import pandas as pd
import numpy as np
from datetime import datetime
import warnings
warnings.filterwarnings('ignore')

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from real_data_fetcher import RealDataFetcher
from simple_technical_analysis import SimpleTechnicalAnalysis
from advanced_stock_screener import AdvancedStockScreener
from technical_analysis_system import TechnicalAnalysisSystem
from fundamental_analysis_system import FundamentalAnalysisSystem
from comprehensive_ranking_system import ComprehensiveRankingSystem
from deep_analysis_system import DeepAnalysisSystem
from trend_prediction_system import TrendPredictionSystem
from investment_recommendation_system import InvestmentRecommendationSystem

class AdvancedAnalysisSystem:
    """高级分析系统 - 动态筛选逻辑"""
    
    def __init__(self):
        self.data_fetcher = RealDataFetcher()
        self.screener = AdvancedStockScreener()
        self.technical_analyzer = TechnicalAnalysisSystem()
        self.fundamental_analyzer = FundamentalAnalysisSystem()
        self.ranking_system = ComprehensiveRankingSystem()
        self.deep_analyzer = DeepAnalysisSystem()
        self.trend_predictor = TrendPredictionSystem()
        self.recommendation_system = InvestmentRecommendationSystem()
        
        # 跟踪状态
        self.target_count = 10  # 目标筛选出10只股票（从5只升级）
        self.min_up_probability = 70  # 最低上涨概率70%
        self.current_candidates = []  # 当前候选股票池
        self.used_stocks = set()  # 已使用过的股票
        self.iteration_count = 0  # 迭代次数
        self.max_iterations = 15  # 最大迭代次数（增加以支持更多股票）
        
        # 存储所有分析过的股票
        self.all_analyzed_stocks = []  # 包括70%以下的股票
        
        # 历史记录管理
        self.history_file = 'analysis_history.csv'
        self.analysis_start_time = datetime.now()
        self.analysis_id = self.generate_analysis_id()
        
    def print_header(self, title):
        """打印标题"""
        print(f"\n{'='*80}")
        print(f"🔥 {title}")
        print(f"{'='*80}")
        
    def print_status(self, message):
        """打印状态信息"""
        print(f"\n🎯 {message}")
        print(f"   迭代次数: {self.iteration_count}/{self.max_iterations}")
        print(f"   目标股票数: {self.target_count}")
        print(f"   已使用股票数: {len(self.used_stocks)}")
        
    def run_advanced_analysis(self):
        """运行高级动态分析系统"""
        self.print_header("高级动态分析系统 - 智能筛选TOP10优质股票")
        
        print(f"🎯 分析目标:")
        print(f"   • 筛选出 {self.target_count} 只最具潜力的股票")
        print(f"   • 要求上涨概率 ≥ {self.min_up_probability}% (已提升至70%)")
        print(f"   • 动态补充机制，确保质量")
        
        # 显示历史分析记录
        self.display_analysis_history()
        
        final_stocks = []
        
        while len(final_stocks) < self.target_count and self.iteration_count < self.max_iterations:
            self.iteration_count += 1
            self.print_status(f"第 {self.iteration_count} 轮分析")
            
            # 步骤1：准备候选股票
            candidates = self.prepare_candidates()
            if not candidates:
                print("❌ 无法获取更多候选股票，分析结束")
                break
                
            # 步骤2：深度分析候选股票
            deep_results = self.analyze_candidates(candidates)
            if not deep_results:
                print("❌ 深度分析失败，跳过本轮")
                continue
                
            # 步骤3：趋势预测筛选
            qualified_stocks = self.predict_and_filter(deep_results)
            
            # 步骤4：添加到最终结果
            for stock in qualified_stocks:
                if len(final_stocks) < self.target_count:
                    final_stocks.append(stock)
                    self.used_stocks.add(stock['code'])
                    
            print(f"✅ 本轮筛选出 {len(qualified_stocks)} 只合格股票")
            print(f"📊 累计筛选出 {len(final_stocks)} 只股票")
            
            if len(final_stocks) >= self.target_count:
                break
                
        # 生成最终报告
        self.generate_final_report(final_stocks)
        
        # 保存到历史记录
        history_record = self.save_analysis_to_history(final_stocks)
        
        return final_stocks
        
    def prepare_candidates(self):
        """准备候选股票 - 智能补充机制"""
        print(f"\n🔍 准备候选股票...")
        
        try:
            # 首先尝试从现有排序结果中获取
            if os.path.exists('final_investment_ranking.csv'):
                ranking_df = pd.read_csv('final_investment_ranking.csv')
                
                # 过滤掉已使用的股票
                available_stocks = ranking_df[~ranking_df['code'].isin(self.used_stocks)]
                
                if len(available_stocks) >= 5:
                    candidates = available_stocks.head(5).to_dict('records')
                    print(f"✅ 从现有排序中获取 {len(candidates)} 只候选股票")
                    return candidates
                else:
                    print(f"⚠️ 现有排序仅剩 {len(available_stocks)} 只股票，需要重新筛选")
                    
            # 重新运行筛选流程
            print(f"🔄 重新运行筛选流程...")
            return self.run_fresh_screening()
            
        except Exception as e:
            print(f"❌ 准备候选股票失败: {e}")
            return []
            
    def run_fresh_screening(self):
        """重新运行筛选流程"""
        try:
            # 第2步：股票筛选
            print(f"   🔍 执行股票筛选...")
            screening_results = self.screener.run_advanced_screening()
            if not screening_results:
                return []
                
            # 过滤掉已使用的股票
            fresh_stocks = [s for s in screening_results if s['code'] not in self.used_stocks]
            
            if len(fresh_stocks) < 5:
                print(f"⚠️ 新筛选结果不足，仅有 {len(fresh_stocks)} 只股票")
                candidates = fresh_stocks
            else:
                candidates = fresh_stocks[:5]
                
            # 第3步：技术分析
            print(f"   📈 执行技术分析...")
            tech_results = self.technical_analyzer.run_technical_analysis()
            
            # 第4步：基本面分析
            print(f"   💰 执行基本面分析...")
            fund_results = self.fundamental_analyzer.run_fundamental_analysis()
            
            # 第5步：综合评分
            print(f"   🎯 执行综合评分...")
            ranking_results = self.ranking_system.run_comprehensive_ranking()
            
            if ranking_results:
                # 过滤并返回前5名
                filtered_results = [r for r in ranking_results if r['code'] not in self.used_stocks]
                return filtered_results[:5] if len(filtered_results) >= 5 else filtered_results
            else:
                return []
                
        except Exception as e:
            print(f"❌ 重新筛选失败: {e}")
            return []
            
    def analyze_candidates(self, candidates):
        """深度分析候选股票"""
        print(f"\n🔬 深度分析 {len(candidates)} 只候选股票...")
        
        deep_results = []
        
        for i, stock in enumerate(candidates, 1):
            print(f"   [{i}/{len(candidates)}] 分析 {stock['code']} {stock.get('name', '')}")
            
            try:
                # 简化深度分析，直接使用现有数据
                result = self.simple_deep_analysis(stock)
                if result:
                    deep_results.append(result)
                    print(f"       ✅ 深度分析完成")
                else:
                    print(f"       ❌ 深度分析失败")
                    
            except Exception as e:
                print(f"       ❌ 分析异常: {e}")
                continue
                
        print(f"✅ 深度分析完成，成功分析 {len(deep_results)} 只股票")
        return deep_results
        
    def simple_deep_analysis(self, stock_info):
        """简化版深度分析"""
        try:
            stock_code = str(stock_info.get('code'))  # 确保转换为字符串
            
            # 获取股票数据
            stock_data = self.data_fetcher.get_stock_data(stock_code, 60)
            if stock_data is None or stock_data.empty:
                return None
                
            # 计算基础指标
            close_prices = stock_data['close']
            current_price = close_prices.iloc[-1]
            
            # 波动率
            returns = close_prices.pct_change().dropna()
            volatility = returns.std() * 100
            
            # 最大回撤
            cumulative = (1 + returns).cumprod()
            running_max = cumulative.expanding().max()
            drawdown = (cumulative - running_max) / running_max
            max_drawdown = abs(drawdown.min() * 100)
            
            # 添加深度分析数据
            enhanced_stock = {
                **stock_info,
                'deep_analysis': {
                    'current_price': current_price,
                    'volatility': volatility,
                    'max_drawdown': max_drawdown,
                    'data_points': len(stock_data)
                }
            }
            
            return enhanced_stock
            
        except Exception as e:
            print(f"简化深度分析失败: {e}")
            return None
        
    def predict_and_filter(self, deep_results):
        """趋势预测和筛选"""
        print(f"\n🔮 趋势预测筛选...")
        
        qualified_stocks = []
        
        for i, stock in enumerate(deep_results, 1):
            stock_code = stock['code']
            stock_name = stock.get('name', '')
            
            print(f"   [{i}/{len(deep_results)}] 预测 {stock_code} {stock_name}")
            
            try:
                # 简化预测逻辑，基于已有数据
                prediction_result = self.simple_trend_prediction(stock)
                
                if prediction_result:
                    up_probability = prediction_result.get('upward_probability', 0)
                    current_price = prediction_result.get('current_price', 0)
                    target_price = prediction_result.get('target_price', 0)
                    
                    print(f"       当前价: {current_price:.2f}元")
                    print(f"       目标价: {target_price:.2f}元")
                    print(f"       上涨概率: {up_probability:.1f}%")
                    
                    if up_probability >= self.min_up_probability:
                        # 合并预测结果到股票信息中
                        qualified_stock = {
                            **stock,
                            'prediction': prediction_result,
                            'up_probability': up_probability,
                            'current_price': current_price,
                            'target_price': target_price
                        }
                        qualified_stocks.append(qualified_stock)
                        print(f"       ✅ 通过筛选 (概率 {up_probability:.1f}% ≥ {self.min_up_probability}%)")
                    else:
                        # 不符合条件的股票也保存到all_analyzed_stocks
                        unqualified_stock = {
                            **stock,
                            'prediction': prediction_result,
                            'up_probability': up_probability,
                            'current_price': current_price,
                            'target_price': target_price,
                            'qualified': False  # 标记为不符合条件
                        }
                        self.all_analyzed_stocks.append(unqualified_stock)
                        print(f"       ❌ 未通过筛选 (概率 {up_probability:.1f}% < {self.min_up_probability}%)")
                        # 不再标记为已使用，保留在结果中
                else:
                    print(f"       ❌ 预测失败")
                    # 预测失败的股票也保存，标记为预测失败
                    failed_stock = {
                        **stock,
                        'up_probability': 0,
                        'current_price': stock.get('current_price', 0),
                        'target_price': stock.get('current_price', 0),
                        'qualified': False,
                        'prediction_failed': True
                    }
                    self.all_analyzed_stocks.append(failed_stock)
                    
            except Exception as e:
                print(f"       ❌ 预测异常: {e}")
                # 预测异常的股票也保存
                error_stock = {
                    **stock,
                    'up_probability': 0,
                    'current_price': stock.get('current_price', 0),
                    'target_price': stock.get('current_price', 0),
                    'qualified': False,
                    'prediction_error': True
                }
                self.all_analyzed_stocks.append(error_stock)
                continue
                
        # 将符合条件的股票也添加到all_analyzed_stocks，并标记为qualified
        for stock in qualified_stocks:
            qualified_stock_copy = stock.copy()
            qualified_stock_copy['qualified'] = True
            self.all_analyzed_stocks.append(qualified_stock_copy)
                
        print(f"📊 预测筛选结果: {len(qualified_stocks)}/{len(deep_results)} 只股票通过")
        
        # 按上涨概率排序
        qualified_stocks.sort(key=lambda x: x.get('up_probability', 0), reverse=True)
        
        return qualified_stocks
        
    def simple_trend_prediction(self, stock_info):
        """简化版趋势预测"""
        try:
            # 获取股票数据
            stock_code = str(stock_info.get('code'))  # 确保转换为字符串
            stock_data = self.data_fetcher.get_stock_data(stock_code, 100)
            
            if stock_data is None or stock_data.empty:
                return None
                
            current_price = stock_data['close'].iloc[-1]
            
            # 基于技术和基本面评分计算概率
            tech_score = stock_info.get('technical_score', 50)
            fund_score = stock_info.get('fundamental_score', 50)
            
            # 计算技术指标
            close_prices = stock_data['close']
            ma5 = close_prices.rolling(5).mean().iloc[-1]
            ma20 = close_prices.rolling(20).mean().iloc[-1]
            
            # 计算RSI
            delta = close_prices.diff()
            gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
            loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
            rs = gain / loss
            rsi = 100 - (100 / (1 + rs))
            current_rsi = rsi.iloc[-1]
            
            # 综合评分计算概率
            base_probability = (tech_score + fund_score) / 2
            
            # 技术调整
            if current_price > ma5 > ma20:
                base_probability += 10
            elif current_price < ma5 < ma20:
                base_probability -= 10
                
            # RSI调整
            if 30 < current_rsi < 70:
                base_probability += 5
            elif current_rsi > 80:
                base_probability -= 15
            elif current_rsi < 20:
                base_probability += 15
                
            # 限制在0-100范围
            upward_probability = max(0, min(100, base_probability))
            
            # 简单目标价计算
            volatility = close_prices.pct_change().std()
            expected_return = (upward_probability / 100 - 0.5) * 0.1  # 基础收益率
            target_price = current_price * (1 + expected_return)
            
            return {
                'current_price': current_price,
                'target_price': target_price,
                'upward_probability': upward_probability,
                'downward_probability': 100 - upward_probability,
                'prediction_method': 'simplified'
            }
            
        except Exception as e:
            print(f"简化预测失败: {e}")
            return None
        
    def generate_final_report(self, final_stocks):
        """生成最终分析报告"""
        self.print_header("最终分析报告")
        
        print(f"🎉 分析完成统计:")
        print(f"   • 总迭代次数: {self.iteration_count}")
        print(f"   • 成功筛选股票数: {len(final_stocks)}")
        print(f"   • 目标完成率: {len(final_stocks)/self.target_count*100:.1f}%")
        
        if final_stocks:
            print(f"\n🏆 最终筛选结果 (TOP{len(final_stocks)}):")
            print(f"{'排名':<4} {'代码':<8} {'名称':<12} {'综合评分':<8} {'上涨概率':<8} {'当前价':<8} {'目标价':<8}")
            print("-" * 80)
            
            for i, stock in enumerate(final_stocks, 1):
                code = stock.get('code', '')
                name = stock.get('name', '')[:8]
                score = stock.get('comprehensive_score', 0)
                prob = stock.get('up_probability', 0)
                current = stock.get('current_price', 0)
                target = stock.get('target_price', 0)
                
                print(f"{i:<4} {code:<8} {name:<12} {score:<8.1f} {prob:<8.1f} {current:<8.2f} {target:<8.2f}")
                
            # 保存结果
            self.save_final_results(final_stocks)
            
            # 生成投资建议
            print(f"\n📋 生成投资建议报告...")
            self.generate_advanced_recommendations(final_stocks)
            
        else:
            print(f"\n❌ 未能筛选出符合条件的股票")
            print(f"   建议调整筛选条件:")
            print(f"   • 降低上涨概率要求 (当前: {self.min_up_probability}%)")
            print(f"   • 扩大筛选范围")
            print(f"   • 调整评分权重")
            
    def save_final_results(self, final_stocks):
        """保存最终结果"""
        try:
            # 保存详细结果
            save_data = []
            for i, stock in enumerate(final_stocks, 1):
                record = {
                    'rank': i,
                    'code': stock.get('code', ''),
                    'name': stock.get('name', ''),
                    'comprehensive_score': stock.get('comprehensive_score', 0),
                    'up_probability': stock.get('up_probability', 0),
                    'current_price': stock.get('current_price', 0),
                    'target_price': stock.get('target_price', 0),
                    'price_change_pct': ((stock.get('target_price', 0) - stock.get('current_price', 0)) / 
                                       stock.get('current_price', 1) * 100),
                    'technical_score': stock.get('technical_score', 0),
                    'fundamental_score': stock.get('fundamental_score', 0),
                    'risk_rating': self.get_risk_rating(stock),
                    'investment_advice': self.get_investment_advice(stock)
                }
                save_data.append(record)
                
            df = pd.DataFrame(save_data)
            filename = 'advanced_analysis_results.csv'
            df.to_csv(filename, index=False, encoding='utf-8-sig')
            print(f"💾 最终结果已保存到 {filename}")
            
            # 更新投资分析报告
            self.update_investment_report(final_stocks)
            
        except Exception as e:
            print(f"❌ 保存结果失败: {e}")
            
    def get_risk_rating(self, stock):
        """获取风险评级"""
        try:
            deep_data = stock.get('deep_analysis', {})
            risk_metrics = deep_data.get('risk_metrics', {})
            volatility = risk_metrics.get('annual_volatility', 20)
            
            if volatility < 15:
                return "低风险"
            elif volatility < 25:
                return "中等风险"
            elif volatility < 35:
                return "较高风险"
            else:
                return "高风险"
        except:
            return "风险未知"
            
    def get_investment_advice(self, stock):
        """获取投资建议"""
        try:
            up_prob = stock.get('up_probability', 0)
            score = stock.get('comprehensive_score', 0)
            
            if up_prob >= 70 and score >= 70:
                return "强烈推荐"
            elif up_prob >= 60 and score >= 60:
                return "推荐买入"
            elif up_prob >= 50:
                return "适量配置"
            elif up_prob >= 40:
                return "谨慎关注"
            else:
                return "暂不建议"
        except:
            return "建议观望"
            
    def generate_advanced_recommendations(self, final_stocks):
        """生成高级投资建议"""
        try:
            if not final_stocks:
                print("❌ 没有股票可以生成建议")
                return
                
            print("📋 正在生成高级投资建议...")
            
            # 计算总体风险评估
            total_risk_score = 0
            for stock in final_stocks:
                volatility = stock.get('deep_analysis', {}).get('volatility', 20)
                if volatility < 15:
                    total_risk_score += 1
                elif volatility < 25:
                    total_risk_score += 2
                else:
                    total_risk_score += 3
                    
            avg_risk = total_risk_score / len(final_stocks)
            
            if avg_risk < 1.5:
                risk_level = "低风险"
            elif avg_risk < 2.5:
                risk_level = "中等风险"
            else:
                risk_level = "较高风险"
                
            # 生成投资组合配置建议
            print("\n🎯 投资组合配置建议:")
            total_prob = sum(s.get('up_probability', 0) for s in final_stocks)
            
            for i, stock in enumerate(final_stocks, 1):
                up_prob = stock.get('up_probability', 0)
                weight = (up_prob / total_prob * 100) if total_prob > 0 else (100 / len(final_stocks))
                advice = self.get_investment_advice(stock)
                
                print(f"   {i}. {stock['code']} {stock.get('name', '')} - 建议权重: {weight:.1f}% ({advice})")
                
            print(f"\n⚠️ 整体风险评估: {risk_level}")
            print(f"📊 预期年化收益: 8-15% (基于历史数据)")
            print(f"🎯 投资周期建议: 3-12个月")
            
            print(f"\n💡 操作建议:")
            print(f"   • 建议分批建仓，每次投入总资金的20-30%")
            print(f"   • 设置止损位：单只股票-15%，组合-10%")
            print(f"   • 设置止盈位：单只股票+20%，组合+15%")
            print(f"   • 每月定期回顾和调整")
            
        except Exception as e:
            print(f"❌ 生成投资建议失败: {e}")

    def update_investment_report(self, final_stocks):
        """更新投资分析报告 - 使用表格格式"""
        try:
            # 加载历史记录用于对比
            history_df = self.load_analysis_history()
            
            # 分类所有分析过的股票
            qualified_stocks = [s for s in self.all_analyzed_stocks if s.get('qualified', False)]
            unqualified_stocks = [s for s in self.all_analyzed_stocks if not s.get('qualified', False)]
            
            # 生成Markdown格式报告
            report_content = f"""# A股中长期投资分析系统 - 高级动态筛选报告

## 📊 分析概况

| 项目 | 数值 |
|------|------|
| 📅 报告日期 | {datetime.now().strftime('%Y年%m月%d日')} |
| 🆔 分析ID | {self.analysis_id} |
| 🎯 筛选标准 | TOP{self.target_count}股票，上涨概率≥{self.min_up_probability}% |
| 🔄 迭代轮次 | {self.iteration_count} |
| ✅ 符合条件股票 | {len(qualified_stocks)}只 |
| ❌ 不符合条件股票 | {len(unqualified_stocks)}只 |
| 📈 目标完成率 | {len(qualified_stocks)/self.target_count*100:.1f}% |
"""
            
            if qualified_stocks:
                avg_prob = sum(s.get('up_probability', 0) for s in qualified_stocks) / len(qualified_stocks)
                avg_score = sum(s.get('comprehensive_score', 0) for s in qualified_stocks) / len(qualified_stocks)
                report_content += f"| 📊 平均上涨概率 | {avg_prob:.1f}% |\n"
                report_content += f"| 🏆 平均综合评分 | {avg_score:.1f}分 |\n"
                report_content += f"| ⚠️ 风险等级 | {self.calculate_portfolio_risk(qualified_stocks)} |\n\n"
            else:
                report_content += "| ❌ 符合条件股票 | 0只 |\n\n"
                
            # 生成符合条件股票表格
            report_content += self.generate_qualified_stocks_table(qualified_stocks)
            
            # 生成不符合条件股票表格
            report_content += self.generate_unqualified_stocks_table(unqualified_stocks)
            
            # 生成投资建议表格
            if qualified_stocks:
                report_content += self.generate_investment_advice_table(qualified_stocks)
            
            # 添加历史对比（如果有的话）
            report_content += self.generate_history_comparison(history_df)
            
            # 添加风险提示
            report_content += self.generate_risk_warning()
            
            # 保存报告
            with open('投资分析报告.md', 'w', encoding='utf-8') as f:
                f.write(report_content)
                
            print(f"📋 投资分析报告已更新 (表格格式，包含所有分析股票)")
            
        except Exception as e:
            print(f"❌ 更新报告失败: {e}")
            import traceback
            traceback.print_exc()
            if not history_df.empty and len(history_df) > 1:
                report_content += f"\n📊 **历史分析对比** (最近5次):\n"
                recent_history = history_df.tail(5)
                
                report_content += f"{'日期':<12} {'阈值':<6} {'成功率':<8} {'平均概率':<10} {'最优股票':<10}\n"
                report_content += f"{'-'*50}\n"
                
                for _, record in recent_history.iterrows():
                    date = record.get('analysis_date', '')
                    threshold = record.get('probability_threshold', 0)
                    success = record.get('success_rate', 0)
                    avg_prob = record.get('avg_probability', 0)
                    top_stock = record.get('top_stock_code', '')
                    
                    report_content += f"{date:<12} {threshold:<6.0f}% {success:<8.1f}% {avg_prob:<10.1f}% {top_stock:<10}\n"
                
                # 趋势分析
                if len(history_df) >= 3:
                    recent_success_rates = history_df.tail(3)['success_rate'].tolist()
                    recent_probabilities = history_df.tail(3)['avg_probability'].tolist()
                    
                    if len(recent_success_rates) >= 2:
                        success_trend = "上升" if recent_success_rates[-1] > recent_success_rates[-2] else "下降"
                        prob_trend = "上升" if recent_probabilities[-1] > recent_probabilities[-2] else "下降"
                        
                        report_content += f"\n📈 **近期趋势分析**:\n"
                        report_content += f"   • 成功率趋势: {success_trend}\n"
                        report_content += f"   • 概率趋势: {prob_trend}\n"
                        
                # 统计摘要
                total_analyses = len(history_df)
                avg_success_rate = history_df['success_rate'].mean()
                avg_historical_prob = history_df['avg_probability'].mean()
                
                report_content += f"\n📋 **历史统计摘要**:\n"
                report_content += f"   • 累计分析次数: {total_analyses}\n"
                report_content += f"   • 历史平均成功率: {avg_success_rate:.1f}%\n"
                report_content += f"   • 历史平均概率: {avg_historical_prob:.1f}%\n"
                
                # 性能对比
                if final_stocks:
                    current_success = (len(final_stocks) / self.target_count) * 100
                    current_avg_prob = sum(s.get('up_probability', 0) for s in final_stocks) / len(final_stocks)
                    
                    report_content += f"\n🆚 **本次vs历史对比**:\n"
                    report_content += f"   • 成功率: {current_success:.1f}% vs {avg_success_rate:.1f}% (历史平均)\n"
                    report_content += f"   • 平均概率: {current_avg_prob:.1f}% vs {avg_historical_prob:.1f}% (历史平均)\n"
                    
                    if current_success > avg_success_rate:
                        report_content += f"   ✅ 本次成功率高于历史平均\n"
                    else:
                        report_content += f"   ⚠️ 本次成功率低于历史平均\n"
                        
                    if current_avg_prob > avg_historical_prob:
                        report_content += f"   ✅ 本次平均概率高于历史平均\n"
                    else:
                        report_content += f"   ⚠️ 本次平均概率低于历史平均\n"
                        
            report_content += f"""
⚠️ **风险评估**:
   • 概率阈值已提升至70%，筛选更加严格
   • 综合风险等级: 中等风险
   • 主要风险因素: 高概率要求可能减少候选数量

🎯 **操作建议**:
   • 70%概率阈值确保高质量选股
   • 建议分批建仓，分散风险
   • 严格执行止盈止损策略
   • 定期跟踪和调整组合

⚠️ **重要提示**:
   • 本报告基于历史数据和模型预测，仅供参考
   • 概率阈值提升可能导致筛选更严格
   • 股市有风险，投资需谨慎
   • 请根据个人风险承受能力投资
"""
            
            with open('投资分析报告.md', 'w', encoding='utf-8') as f:
                f.write(report_content)
                
            print(f"📋 投资分析报告已更新 (包含历史对比)")
            
        except Exception as e:
            print(f"❌ 更新报告失败: {e}")

    def generate_analysis_id(self):
        """生成分析ID"""
        return f"ADV_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
        
    def load_analysis_history(self):
        """加载历史分析记录"""
        try:
            if os.path.exists(self.history_file):
                return pd.read_csv(self.history_file)
            else:
                # 创建新的历史记录文件
                return pd.DataFrame(columns=[
                    'analysis_id', 'analysis_date', 'analysis_time', 
                    'probability_threshold', 'target_stocks', 'iterations_used',
                    'stocks_found', 'success_rate', 'avg_probability', 
                    'avg_score', 'risk_level', 'top_stock_code', 
                    'top_stock_probability', 'duration_seconds'
                ])
        except Exception as e:
            print(f"❌ 加载历史记录失败: {e}")
            return pd.DataFrame()
            
    def save_analysis_to_history(self, final_stocks):
        """保存当前分析到历史记录"""
        try:
            # 计算分析统计数据
            analysis_end_time = datetime.now()
            duration = (analysis_end_time - self.analysis_start_time).total_seconds()
            
            stocks_found = len(final_stocks)
            success_rate = (stocks_found / self.target_count) * 100
            
            if final_stocks:
                avg_probability = sum(s.get('up_probability', 0) for s in final_stocks) / len(final_stocks)
                avg_score = sum(s.get('comprehensive_score', 0) for s in final_stocks) / len(final_stocks)
                top_stock = final_stocks[0]
                top_stock_code = top_stock.get('code', '')
                top_stock_probability = top_stock.get('up_probability', 0)
            else:
                avg_probability = 0
                avg_score = 0
                top_stock_code = ''
                top_stock_probability = 0
                
            # 创建历史记录
            history_record = {
                'analysis_id': self.analysis_id,
                'analysis_date': self.analysis_start_time.strftime('%Y-%m-%d'),
                'analysis_time': self.analysis_start_time.strftime('%H:%M:%S'),
                'probability_threshold': self.min_up_probability,
                'target_stocks': self.target_count,
                'iterations_used': self.iteration_count,
                'stocks_found': stocks_found,
                'success_rate': success_rate,
                'avg_probability': avg_probability,
                'avg_score': avg_score,
                'risk_level': self.calculate_portfolio_risk(final_stocks),
                'top_stock_code': top_stock_code,
                'top_stock_probability': top_stock_probability,
                'duration_seconds': duration
            }
            
            # 加载现有历史记录
            history_df = self.load_analysis_history()
            
            # 添加新记录
            new_record_df = pd.DataFrame([history_record])
            history_df = pd.concat([history_df, new_record_df], ignore_index=True)
            
            # 保存到文件
            history_df.to_csv(self.history_file, index=False, encoding='utf-8-sig')
            print(f"📊 分析记录已保存到历史文件: {self.history_file}")
            
            return history_record
            
        except Exception as e:
            print(f"❌ 保存历史记录失败: {e}")
            return None
            
    def calculate_portfolio_risk(self, stocks):
        """计算投资组合风险等级"""
        if not stocks:
            return "N/A"
            
        try:
            total_risk_score = 0
            for stock in stocks:
                deep_data = stock.get('deep_analysis', {})
                volatility = deep_data.get('volatility', 20)
                
                if volatility < 15:
                    total_risk_score += 1
                elif volatility < 25:
                    total_risk_score += 2
                elif volatility < 35:
                    total_risk_score += 3
                else:
                    total_risk_score += 4
                    
            avg_risk = total_risk_score / len(stocks)
            
            if avg_risk < 1.5:
                return "低风险"
            elif avg_risk < 2.5:
                return "中等风险"
            elif avg_risk < 3.5:
                return "较高风险"
            else:
                return "高风险"
                
        except:
            return "风险未知"
            
    def display_analysis_history(self):
        """显示历史分析记录"""
        try:
            history_df = self.load_analysis_history()
            
            if history_df.empty:
                print("📊 暂无历史分析记录")
                return
                
            print("\n" + "="*100)
            print("📊 历史分析记录")
            print("="*100)
            
            # 显示最近10次记录
            recent_history = history_df.tail(10)
            
            print(f"{'日期':<12} {'时间':<8} {'阈值':<6} {'找到':<4} {'成功率':<8} {'平均概率':<8} {'最优股票':<10} {'耗时(秒)':<8}")
            print("-" * 100)
            
            for _, record in recent_history.iterrows():
                date = record.get('analysis_date', '')
                time = record.get('analysis_time', '')
                threshold = record.get('probability_threshold', 0)
                found = record.get('stocks_found', 0)
                success = record.get('success_rate', 0)
                avg_prob = record.get('avg_probability', 0)
                top_stock = record.get('top_stock_code', '')
                duration = record.get('duration_seconds', 0)
                
                print(f"{date:<12} {time:<8} {threshold:<6.0f}% {found:<4} {success:<8.1f}% {avg_prob:<8.1f}% {top_stock:<10} {duration:<8.1f}")
                
            # 统计摘要
            print("\n📈 历史分析统计摘要:")
            total_analyses = len(history_df)
            avg_success_rate = history_df['success_rate'].mean()
            avg_probability = history_df['avg_probability'].mean()
            best_analysis = history_df.loc[history_df['success_rate'].idxmax()] if not history_df.empty else None
            
            print(f"   • 总分析次数: {total_analyses}")
            print(f"   • 平均成功率: {avg_success_rate:.1f}%")
            print(f"   • 平均上涨概率: {avg_probability:.1f}%")
            
            if best_analysis is not None:
                print(f"   • 最佳分析: {best_analysis['analysis_date']} (成功率: {best_analysis['success_rate']:.1f}%)")
                
            print("="*100)
            
        except Exception as e:
            print(f"❌ 显示历史记录失败: {e}")

    def generate_qualified_stocks_table(self, qualified_stocks):
        """生成符合条件股票表格"""
        if not qualified_stocks:
            return "## 🎯 符合条件股票 (上涨概率≥70%)\n\n暂无符合条件的股票\n\n"
            
        content = f"## 🎯 符合条件股票 (上涨概率≥70%) - 共{len(qualified_stocks)}只\n\n"
        content += "| 排名 | 股票代码 | 股票名称 | 当前价格 | 目标价格 | 预期收益 | 上涨概率 | 综合评分 | 投资建议 |\n"
        content += "|------|----------|----------|----------|----------|----------|----------|----------|----------|\n"
        
        # 按上涨概率排序
        sorted_stocks = sorted(qualified_stocks, key=lambda x: x.get('up_probability', 0), reverse=True)
        
        for i, stock in enumerate(sorted_stocks, 1):
            code = stock.get('code', '')
            name = stock.get('name', '')
            current_price = stock.get('current_price', 0)
            target_price = stock.get('target_price', 0)
            expected_return = ((target_price - current_price) / current_price * 100) if current_price > 0 else 0
            up_prob = stock.get('up_probability', 0)
            score = stock.get('comprehensive_score', 0)
            advice = self.get_investment_advice(stock)
            
            content += f"| {i} | {code} | {name} | {current_price:.2f}元 | {target_price:.2f}元 | {expected_return:+.1f}% | {up_prob:.1f}% | {score:.1f}分 | {advice} |\n"
        
        content += "\n"
        return content
        
    def generate_unqualified_stocks_table(self, unqualified_stocks):
        """生成不符合条件股票表格"""
        if not unqualified_stocks:
            return "## ❌ 不符合条件股票 (上涨概率<70%)\n\n暂无此类股票\n\n"
            
        content = f"## ❌ 不符合条件股票 (上涨概率<70%) - 共{len(unqualified_stocks)}只\n\n"
        content += "| 序号 | 股票代码 | 股票名称 | 当前价格 | 目标价格 | 预期收益 | 上涨概率 | 综合评分 | 不推荐原因 |\n"
        content += "|------|----------|----------|----------|----------|----------|----------|----------|----------|\n"
        
        # 按上涨概率排序（从高到低）
        sorted_stocks = sorted(unqualified_stocks, key=lambda x: x.get('up_probability', 0), reverse=True)
        
        for i, stock in enumerate(sorted_stocks, 1):
            code = stock.get('code', '')
            name = stock.get('name', '')
            current_price = stock.get('current_price', 0)
            target_price = stock.get('target_price', 0)
            expected_return = ((target_price - current_price) / current_price * 100) if current_price > 0 else 0
            up_prob = stock.get('up_probability', 0)
            score = stock.get('comprehensive_score', 0)
            
            # 判断不推荐原因
            reason = "上涨概率不足"
            if stock.get('prediction_failed', False):
                reason = "预测失败"
            elif stock.get('prediction_error', False):
                reason = "预测异常"
            elif up_prob < 50:
                reason = "上涨概率过低"
            elif score < 50:
                reason = "综合评分偏低"
            
            content += f"| {i} | {code} | {name} | {current_price:.2f}元 | {target_price:.2f}元 | {expected_return:+.1f}% | {up_prob:.1f}% | {score:.1f}分 | {reason} |\n"
        
        content += "\n"
        return content
    
    def generate_investment_advice_table(self, qualified_stocks):
        """生成投资建议表格"""
        content = "## 💼 投资建议与仓位配置\n\n"
        content += "| 股票代码 | 股票名称 | 建议仓位 | 风险等级 | 预期收益 | 操作建议 | 止损位 |\n"
        content += "|----------|----------|----------|----------|----------|----------|--------|\n"
        
        total_prob = sum(s.get('up_probability', 0) for s in qualified_stocks)
        
        for stock in qualified_stocks:
            code = stock.get('code', '')
            name = stock.get('name', '')
            up_prob = stock.get('up_probability', 0)
            current_price = stock.get('current_price', 0)
            target_price = stock.get('target_price', 0)
            
            # 计算建议仓位（基于上涨概率权重）
            if total_prob > 0:
                weight = (up_prob / total_prob * 100)
                # 限制单只股票最大仓位15%
                weight = min(weight, 15)
            else:
                weight = 100 / len(qualified_stocks)
                
            # 风险等级
            risk_level = self.get_risk_level(stock)
            
            # 预期收益
            expected_return = ((target_price - current_price) / current_price * 100) if current_price > 0 else 0
            
            # 操作建议
            advice = self.get_detailed_advice(stock)
            
            # 止损位（当前价格的10%以下）
            stop_loss = current_price * 0.9
            
            content += f"| {code} | {name} | {weight:.1f}% | {risk_level} | {expected_return:+.1f}% | {advice} | {stop_loss:.2f}元 |\n"
        
        content += "\n"
        return content
    
    def generate_history_comparison(self, history_df):
        """生成历史对比表格"""
        if history_df.empty or len(history_df) < 2:
            return "## 📊 历史分析对比\n\n暂无足够历史数据进行对比\n\n"
            
        content = "## 📊 历史分析对比\n\n"
        content += "| 分析日期 | 目标数量 | 成功数量 | 成功率 | 平均概率 | 平均评分 |\n"
        content += "|----------|----------|----------|--------|----------|----------|\n"
        
        # 显示最近5次记录
        recent_history = history_df.tail(5)
        
        for _, record in recent_history.iterrows():
            date = record.get('analysis_date', '')
            target = record.get('target_stocks', 0)
            found = record.get('stocks_found', 0)
            success_rate = record.get('success_rate', 0)
            avg_prob = record.get('avg_probability', 0)
            avg_score = record.get('avg_score', 0)
            
            content += f"| {date} | {target} | {found} | {success_rate:.1f}% | {avg_prob:.1f}% | {avg_score:.1f}分 |\n"
            
        content += "\n"
        return content
    
    def generate_risk_warning(self):
        """生成风险提示"""
        return """## ⚠️ 风险提示与免责声明

### 投资风险提示
- **市场风险**: 股票价格受多种因素影响，存在下跌风险
- **预测限制**: 上涨概率基于历史数据和技术指标，不构成收益保证
- **流动性风险**: 部分股票可能存在交易不活跃的情况
- **政策风险**: 相关政策变化可能影响股票表现

### 使用建议
- 本报告仅供投资参考，不构成投资建议
- 建议结合自身风险承受能力制定投资策略
- 严格执行止损策略，控制单笔损失
- 分散投资，避免集中持仓风险

### 免责声明
- 投资有风险，入市需谨慎
- 过往业绩不代表未来表现
- 请在专业人士指导下进行投资决策

---
*报告生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*
*分析系统版本: 高级动态筛选v2.0*
"""
    
    def get_risk_level(self, stock):
        """获取风险等级"""
        up_prob = stock.get('up_probability', 0)
        score = stock.get('comprehensive_score', 0)
        
        if up_prob >= 80 and score >= 75:
            return "低风险"
        elif up_prob >= 75 and score >= 70:
            return "中低风险"
        elif up_prob >= 70 and score >= 60:
            return "中等风险"
        else:
            return "中高风险"
    
    def get_detailed_advice(self, stock):
        """获取详细操作建议"""
        up_prob = stock.get('up_probability', 0)
        
        if up_prob >= 85:
            return "积极买入，分批建仓"
        elif up_prob >= 80:
            return "适量买入，谨慎观察"
        elif up_prob >= 75:
            return "小量试探，等待机会"
        else:
            return "观望为主，等待确认"

    # ...existing code...
def run_advanced_analysis():
    """运行高级分析系统主函数"""
    print("="*80)
    print("         A股中长期投资分析系统 - 高级动态筛选模式")
    print("="*80)
    print(f"🕐 开始时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"🎯 目标: 智能筛选出5只最具潜力的投资标的")
    print(f"📋 新功能: ")
    print(f"   • 第6步分析TOP5股票(原来是TOP3)")
    print(f"   • 第7步预测，上涨概率<70%的股票自动剔除(从40%提升)")
    print(f"   • 不足5只时自动补充，确保质量")
    print(f"   • 智能迭代，直到找到5只合格股票")
    print(f"   • 历史分析记录，对比分析性能")
    print("="*80)
    
    try:
        # 创建高级分析系统
        advanced_system = AdvancedAnalysisSystem()
        
        # 执行分析
        final_stocks = advanced_system.run_advanced_analysis()
        
        print(f"\n{'='*80}")
        print(f"🎉 高级动态筛选完成！")
        print(f"📊 最终筛选出 {len(final_stocks)} 只优质股票")
        print(f"🕐 完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
        print(f"{'='*80}")
        
        return final_stocks
        
    except Exception as e:
        print(f"❌ 高级分析系统执行失败: {e}")
        return []

if __name__ == "__main__":
    results = run_advanced_analysis()
